//
//  XZGMapTableViewController.m
//  mapTest
//
//  Created by xiangzhigang on 16/5/23.
//  Copyright © 2016年 xiangzhi gang. All rights reserved.
//

#import "XZGMapTableViewController.h"
#import "MapViewController.h"
#import "MapViewController.h"

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

#import "XZGMap.h"

#import "XZGAddressTableViewCell.h"
@interface XZGMapTableViewController (){
    
    NSString *phone;
    
}


@end

static NSString *XZGAddressTableViewCellID = @"XZGAddressTableViewCell";

@implementation XZGMapTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[XZGAddressTableViewCell class] forCellReuseIdentifier:XZGAddressTableViewCellID];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSLog(@"%lu",(unsigned long)self.mapArr.count);
    return self.mapArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XZGAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XZGAddressTableViewCellID forIndexPath:indexPath];
//    for (MKMapItem *item in self.mapArr) {
//        cell.textLabel.text = item.name;
//        
//    }
    MKMapItem *item = self.mapArr[indexPath.row];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSString *ad1 = item.placemark.locality;
    NSString *ad2 = item.placemark.subLocality;
    NSString *ad3 = item.placemark.thoroughfare;
    NSString *ad;
    ad = [ad1 stringByAppendingFormat:@",%@,%@",ad2,ad3];
    
    //NSLog(@"%@",item.placemark.addressDictionary);
    
       // NSLog(@"%lf,%lf",self.coordinate1.latitude,self.coordinate1.longitude);

    cell.adNameLabel.text = [@"  " stringByAppendingFormat:@"    %@",item.name];
    
    phone = [item.phoneNumber substringFromIndex:9];
    
    
    
    if (item.placemark.thoroughfare) {
        cell.adAddressLabel.text = ad;

    }else {
        cell.adAddressLabel.text = @"     请用百度地图查询";
    }
    
    [cell.phone_btn addTarget:self action:@selector(clickPhone) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.location_btn addTarget:self action:@selector(clickLocation:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.location_btn.tag = indexPath.row;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)clickPhone {
    
    NSString *test = [@"tel:" stringByAppendingFormat:@"%@",phone];
    
    NSString *allString = [NSString stringWithFormat:@"%@",test];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:allString]];
    
}

- (void)clickLocation:(UIButton *)btn {
    
    MKMapItem *item = self.mapArr[btn.tag];
    XZGMap *xm = [[XZGMap alloc] init];
    xm.coordinate2 = item.placemark.coordinate;
    [self.navigationController pushViewController:xm animated:YES];
    
}

- (NSMutableArray *)mapArr{
    if (_mapArr == nil) {
        _mapArr = [NSMutableArray array];
        
    }
    return _mapArr;
}

@end
